//自己封装的方法----------------------------------------------------------------------------------------------------------------------
/**
 * 【 多点找图点击 】
 *
 * @author yangjunxiong
 * @date 2022/1/9 15:09
 * @param {Array} fileNames ["能量图1.png","能量图2.png"]
 * @param {number} x
 * @param {number} y
 * @param {number} ex
 * @param {number} ey
 * @param {number} plusx 偏移量x
 * @param {number} plusy 偏移量y
 * @return
 **/
function findImagePlus(fileNames, x, y, ex, ey, plusx, plusy) {
    plusx = plusx || 0;
    plusy = plusy || 0;
    for (let i = 0; i < fileNames.length; i++) {
        smallTmplate = readResAutoImage(fileNames[i]);
        let points = image.findImageEx(smallTmplate, x, y, ex, ey, 0.8, 0.9, 1, 5);
        let pointsStr = JSON.stringify(points || null);
        logd("findImageClickArray:开始," + fileNames[i] + ",pointsStr:" + pointsStr + ",plusx:" + plusx + ",plusy:" + plusy);
        if (points) {
            let clickX = points[0].left + plusx;
            let clickY = points[0].top + plusy;
            if (clickPointRnd((clickX - 10), (clickY - 10), (clickX + 10), (clickY + 10))) {
                sleep(random(150,350));
                image.recycle(smallTmplate);
                logd("findImageClickArray:点击成功,fileName:" + fileNames[i] + ",pointsStr:" + pointsStr + ",clikcX:" + clickX + ",clickY:" + clickY);
                return true;
            } else {
                logd("findImageClickArray:找到点击失败,fileName:" + fileNames[i] + ",pointsStr:" + pointsStr + ",clikcX:" + clickX + ",clickY:" + clickY);
                image.recycle(smallTmplate);
                return false;
            }
        }
        sleep(random(10, 100))
        logw("findImageClickArray:未找到,fileName:" + fileNames[i]);
        image.recycle(smallTmplate)
    }
    return false;
}

/**
 * 【 多点找色 】
 *
 * @author yangjunxiong
 * @date 2022/1/10 15:03
 * @param {string} msg 日志
 * @param {Array} colors [{
        firstColor: "#DEFF00-#101010",
        multiColor: "-42|-70|#E2FDB0-#101010,44|-64|#AEFD03-#101010,-30|8|#BFFC00-#101010,36|8|#DCFC7D-#101010"
    }, {
        firstColor: "#D9FE00-#101010",
        multiColor: "-55|-47|#D4FB88-#101010,-10|-98|#F3FFE4-#101010,23|6|#DDFD7E-#101010,0|49|#2FA64A-#101010"
    }]
 * @param {number} x
 * @param {number} y
 * @param {number} ex
 * @param {number} ey
 * @param {number} plusx 偏移量x
 * @param {number} plusy 偏移量y
 **/
function findMultiColorPlus(msg, colors, x, y, ex, ey, plusx, plusy) {
    logd(msg);
    plusx = plusx || 0;
    plusy = plusy || 0;
    for (let i = 0; i < colors.length; i++) {
        let colorsStr = JSON.stringify(colors[i]);
        let points = image.findMultiColorEx(colors[i].firstColor, colors[i].multiColor, 0.9, x, y, ex, ey, 1, 1);
        let pointsStr = JSON.stringify(points || null);
        logd("findMultiColorClickArray.firstColor[" + i + "]:开始,pointsStr:" + pointsStr + ",plusx:" + plusx + ",plusy:" + plusy);
        if (points) {
            let clickX = points[0].x + plusx;
            let clickY = points[0].y + plusy;
            if (clickPointRnd((clickX - 10), (clickY - 10), (clickX + 10), (clickY + 10))) {
                sleep(random(150,350));
                logd("findMultiColorClickArray.firstColor[" + i + "]:点击成功," + colorsStr + ",points" + pointsStr + ",clickX:" + clickX + ",clickY:" + clickY);
                image.recycle(smallTmplate);
                return true;
            } else {
                logd("findMultiColorClickArray.firstColor[" + i + "]:点击失败," + colorsStr + ",points" + pointsStr + ",clickX:" + clickX + ",clickY:" + clickY);
                image.recycle(smallTmplate);
                return false;
            }
        }
        image.recycle(smallTmplate);
        logw("findMultiColorClickArray.firstColor[" + i + "]:未找到," + colorsStr);
    }
    return false;
}

/**
 * 【 查找节点并偏移点击 】
 *
 * @author yangjunxiong
 * @param {S} selector 选择器
 * @param {number} plusx
 * @param {number} plusy
 * @param {number} point 指向偏移量 1 左上 2 右上 3左下 4右下
 * @param {Boolean?} isClick 是否点击
 * @return {Boolean} true/false;全局jsonObj  g_ret;
 * @example
 * findNodePlus(id("J_tree_dialog_wrap"), 4, random(-160, -90), random(-100, -60), true)
 */
function findNodePlus(selector, point, plusx, plusy, isClick) {
    plusx = plusx || 0;
    plusy = plusy || 0;
    if (typeOf(selector) !== "S") {
        logw("防SB提示,findNode,传参类型不对:" + selector)
        return false;
    }
    g_ret = selector.getOneNodeInfo(0);
    if (g_ret) {
        if (isClick) {
            logd("findNodeLeftAndTop.selector:" + JSON.stringify(selector));
            logd("findNodeLeftAndTop.g_ret.bounds:" + JSON.stringify(g_ret.bounds));
            logd("findNodeLeftAndTop.clickPoint:plusx:" + plusx + ",plusy:" + plusy);
            let x = g_ret.bounds.left + plusx;
            let y = g_ret.bounds.top + plusy;
            let ex = g_ret.bounds.right + plusx;
            let ey = g_ret.bounds.bottom + plusy;
            let randomX;
            let randomY;
            switch (point) { // point 指向偏移量 1 左上 2 右上 3左下 4右下
                case 1:
                    randomX = random(x, x + 10);
                    randomY = random(y, y + 10);
                    break;
                case 2:
                    randomX = random(ex - 10, ex - 1);
                    randomY = random(y + 1, y + 10);
                    break;
                case 3:
                    randomX = random(x + 1, x + 10);
                    randomY = random(ey - 10, ey - 1);
                    break;
                case 4:
                    randomX = random(ex - 10, ex - 1);
                    randomY = random(ey - 10, ey - 1);
                    break;
            }
            logd("findNodeLeftAndTop.clickPoint:randomX:" + randomX + ",randomY:" + randomY);
            if (clickPoint(randomX, randomY)) {
                sleep(random(150,350));
            }
        }
        return true;
    }
    return false;
}

/**
 *
 * 根据某一个真机分辨率计算出因数,从而计算出点击位置
 * @param {string} name 区分保存的分辨率比例
 * @param {number} realX
 * @param {number} realY
 */
function getAutoXY(name, realX, realY) {
    // 拿到对应数据
    // 获取屏幕高度宽度
    let width = device.getScreenWidth();
    let height = device.getScreenHeight();
    // 拿到比例获取对应的坐标点
    let x = getScale(name, realX, "x", width, height) * width / 10
    let y = getScale(name, realY, "y", width, height) * height / 10
    return [x, y]
}


/**
 * 获取分辨率比例
 * @param {string}  name 坐标名称 区分
 * @param {number} num 坐标点
 * @param {String}  type 坐标类型  x:y
 * @param {number} width  当前设备宽
 * @param {number} height  高
 * @returns {number} 返回当前计算后的比例  所有手机保持相同比例实现全分辨率执行
 */
function getScale(name, num, type, width, height) {
    // 计算后保存对于坐标点比例
    let data = readResString("xy.js") || {};
    let dataKey = name + "_" + type
    // 判断是否是计算比例的手机
    if (width !== 1080 || height !== 2200) {
        // 不是测试手机必须使用比例
        if (autoXY !== null) {
            return autoXY[dataKey]
        }
        let parse = JSON.parse((data + "").split("=")[1]);
        autoXY = parse;
        let datum = parse[dataKey];
        if (datum) {
            logd("datum:" + datum)
            return datum;
        }
        toast("当前不存在对应屏幕比例")
    }
    let numXy;
    if (type === "x") {
        numXy = num * 10 / width
    }
    if (type === "y") {
        numXy = num * 10 / height
    }
    if (autoXY === null) {
        autoXY = {};
    }
    autoXY[dataKey] = numXy;
    return numXy
}

/**
 * 【 远程获取图片并插入图库 】
 *
 * @author yangjunxiong
 * @date 2022/1/13 12:01
 * @return
 * @param {string} url
 * @param {string} fileName
 * 例子 downloadFileVideo("http://yjx-show.top:88/down/p95iYXur52uL", "qq.mp4");
 **/
function downloadFileVideo(url, fileName) {
    fileName = "/sdcard/" + fileName;
    let fileResult = http.downloadFileDefault(url, fileName, {"User-Agent": laoleng.http.randomUA()});
    utils.insertVideoToAlbum(fileName);//视频插入相册
    return fileResult;
}

/**
 * 【 远程获取视频并插入图库 】
 *
 * @author yangjunxiong
 * @date 2022/1/13 12:01
 * @return
 * @param {string} url
 * @param {string} fileName
 * 例子 downloadFileImage("http://yjx-show.top:88/down/ZjI9JNRqVMxo","未配置图片.png");
 **/
function downloadFileImage(url, fileName) {
    fileName = "/sdcard/" + fileName;
    let fileResult = http.downloadFileDefault(url, fileName, {"User-Agent": laoleng.http.randomUA()});
    utils.insertImageToAlbum(fileName);//图片插入相册
    return fileResult;
}
